package com.dayatang.android.map;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Activity.SubprojectMapActivity;
import com.dayatang.android.Model.Route.GeoPointChar;
import com.dayatang.android.Model.Route.Leg;
import com.dayatang.android.Model.Route.Step;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class StepOverItem extends ItemizedOverlay<OverlayItem> {

	private Drawable marker;
	private SubprojectMapActivity context;
	
	private TextView stepTextView;
	private List<OverlayItem> geoList = new ArrayList<OverlayItem>();
	
	private View stepView;
	public StepOverItem(Drawable defaultMarker, Context context, List<Leg> legs) {
		super(boundCenter(defaultMarker));
		this.marker = defaultMarker;
		this.context = (SubprojectMapActivity) context;
		InitPopView();
		InitGeoPoints(legs);
		populate();
	}

	private void InitGeoPoints(List<Leg> legs) {
		for (Leg leg : legs) {
			for (Step step : leg.getSteps()) {
				GeoPointChar geoPointChar = step.getStart_location();
				GeoPoint geoPoint = new GeoPoint((int) (Double.valueOf(geoPointChar.getLat())*1E6), (int) (Double.valueOf(geoPointChar.getLng())*1E6));
				geoList.add(new OverlayItem(geoPoint, "", step.getHtml_instructions() +"<span style="+'"'+"color:"+R.color.red+'"'+">("+ step.getDistance().getText()+")</span>"));
			}
		}
	}
	
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		Projection projection = mapView.getProjection();
		for (int index = size() - 1; index >= 0; index--) {
			OverlayItem overLayItem = getItem(index);
			String title = overLayItem.getTitle();
			Point point = projection.toPixels(overLayItem.getPoint(), null); 
			Paint paintCircle = new Paint();
			paintCircle.setColor(Color.RED);
			canvas.drawCircle(point.x, point.y, 5, paintCircle); // 画圆

			Paint paintText = new Paint();
			paintText.setColor(Color.BLACK);
			paintText.setTextSize(15);
			canvas.drawText(title, point.x, point.y - 25, paintText); // 绘制文本
		}
		super.draw(canvas, mapView, shadow);
		boundCenter(marker);
	}
	
	protected boolean onTap(int index) {
		setFocus(geoList.get(index));
		MapView.LayoutParams geoLP = (MapView.LayoutParams) stepView.getLayoutParams();
		geoLP.point = geoList.get(index).getPoint();
		context.mapView.getController().animateTo(geoLP.point);
		context.mapView.updateViewLayout(stepView, geoLP);
		stepView.setVisibility(View.VISIBLE);
		InitView(geoList.get(index).getSnippet());
		return true;
	}
	
	private void InitView(String data) {
		stepTextView = (TextView) stepView.findViewById(R.id.map_step);
		stepTextView.setText(Html.fromHtml(data));
	}
	
	private void InitPopView() {
		stepView = context.getLayoutInflater().inflate(R.layout.step_pop, null);
	    context.mapView.addView(stepView, new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT, null, MapView.LayoutParams.BOTTOM_CENTER));
	    stepView.setVisibility(View.GONE);
	}
	
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		stepView.setVisibility(View.GONE);
		return super.onTouchEvent(event, mapView);
	}
	
	protected OverlayItem createItem(int i) {
		return geoList.get(i);
	}

	public int size() {
		return geoList.size();
	}
	
}
